using System;
using System.Collections.Generic;
using System.IO;
using Conusic.Properties;

namespace Conusic
{
    /// <summary>
    ///   Realization of one Audio object
    /// </summary>
    public class AudioRecord
    {
        private static readonly Dictionary<char, bool> invalidCharsFilename = GetInvalidFileNameCharsDictionary();

        public AudioRecord()
        {
        }

        public AudioRecord(string artist, string compositionName)
        {
            Artist = artist;
            CompositionName = compositionName;
        }

        public string Artist { get; set; }
        public string CompositionName { get; set; }

        /// <summary>
        ///   Duration in seconds.
        /// </summary>
        public int Duration { get; set; }

        public string Url { get; set; }
        public int VkontakteLyricId { get; set; }

        /// <summary>
        ///   Correct filename path to song.
        /// </summary>
        public string Filename
        {
            get
            {
                string filename = Artist + " - " + CompositionName + ".mp3";
                return EscapeFilename(filename);
            }
        }

        /// <summary>
        ///   Gets the duration in format Minutes:second.
        /// </summary>
        public string DurationMs
        {
            get { return String.Format("{0}:{1:D2}", Duration / 60, Duration % 60); }
        }

        /// <summary>
        ///   Gets the concat of artist and composition.
        /// </summary>
        public string ArtistAndComposition
        {
            get { return Artist + " " + CompositionName; }
        }

        public string FullFilename
        {
            get
            {
                string currentFileName = Filename;
                var filename = String.Format("{0}{1}{2}",
                     Settings.Default.DownloadDirectory,
                     System.IO.Path.DirectorySeparatorChar,
                     currentFileName
                     );
                int i = 1;
                while (File.Exists(filename))
                {
                    filename = String.Format("{0}{1}{2}_{3}.{4}",
                        Settings.Default.DownloadDirectory,
                        System.IO.Path.DirectorySeparatorChar,
                        Path.GetFileNameWithoutExtension(currentFileName),
                        i,
                        Path.GetExtension(currentFileName)
                    );
                    i++;
                }
                return filename;
            }
        }

        /// <summary>
        ///   Implement ToDictionary in .net 3.5
        /// </summary>
        /// <returns></returns>
        private static Dictionary<char, bool> GetInvalidFileNameCharsDictionary()
        {
            var result = new Dictionary<char, bool>();
            char[] invalidChars = Path.GetInvalidFileNameChars();
            foreach (char invalidChar in invalidChars)
            {
                result.Add(invalidChar, true);
            }
            return result;
        }

        /// <summary>
        ///   Escapes the filename.
        /// </summary>
        /// <param name = "filename">The filename.</param>
        /// <returns></returns>
        public static string EscapeFilename(string filename)
        {
            for (int i = 0; i < filename.Length; i++)
            {
                if (invalidCharsFilename.ContainsKey(filename[i]))
                {
                    filename = filename.Remove(i, 1);
                    if (i >= 0) i--;
                }
            }
            return filename;
        }

        public static string JsonEscape(string input)
        {
            input = input.Replace("\\", "\\\\");
            input = input.Replace("\"", "\\\"");
            return input;
        }

        public string Json()
        {
            // {"name" : "name", "artist" : "artist", "duration":duration, "url":"url","lid":lid}
            return
                String.Format(
                    "{{\"name\" : \"{0}\", \"artist\" : \"{1}\", \"duration\":{2}, \"url\":\"{3}\",\"lid\":{4}}}",
                    JsonEscape(CompositionName),
                    JsonEscape(Artist),
                    Duration,
                    JsonEscape(Url),
                    VkontakteLyricId);
        }

        public override string ToString()
        {
            return ArtistAndComposition;
        }
    }
}